/**
 * Copyright 2009-2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.model.geom;

import java.util.ArrayList;
import java.util.List;

import chunmap.model.coord.Transform;
import chunmap.model.coord.Position;
import chunmap.model.coord.Coordinate2D;
import chunmap.model.coord.Coordinate3D;
import chunmap.model.elem.Envelope;

/**
 * 点
 * 
 * @author chunquedong
 * 
 */
public class GeoPoint extends AbstractGeometry implements Comparable<GeoPoint> {

	private final Position coordinate;

	public GeoPoint(double x, double y) {
		this(new Coordinate2D(x, y));
	}

	public GeoPoint(Position coord) {
		coordinate=coord;
	}
	// ------------------------------------------------------------

	public double getX() {
		return coordinate.getX();
	}

	public double getY() {
		return coordinate.getY();
	}

	public Position getCoordinate() {
		return coordinate;
	}

	public boolean isPoint3D() {
		if (coordinate instanceof Coordinate3D)
			return true;
		else
			return false;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GeoPoint p = (GeoPoint) obj;
		return coordinate.equals(p.coordinate);
	}

	public boolean approximateEquals(GeoPoint obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		return coordinate.approximateEquals(obj.coordinate);
	}

	@Override
	public int hashCode() {
		return coordinate.hashCode();
	}

	@Override
	public String toString() {
		return "POINT(" + getX() + " " + getY() + ")";
	}

	@Override
	protected Envelope calculateEnvelop() {
		return new Envelope(getX(), getY(), getX(), getY());
	}

	@Override
	public GeometryType getGeometryType() {
		return GeometryType.Point;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(GeoPoint other) {
		return coordinate.compareTo(other.coordinate);
	}

	@Override
	public Geometry getBoundary() {
		return null;
	}

	@Override
	public GeometryCollection toGeometryCollection() {
		List<GeoPoint> gs = new ArrayList<GeoPoint>();
		gs.add(this);
		return new MultiPoint(gs);
	}

	@Override
	public Geometry transform(Transform transf) {
		return new GeoPoint(transf.convert(this.coordinate));
	}

	@Override
	protected boolean isSimple() {
		return true;
	}

}
